<template>
	<el-dialog title="故障处理" v-model="visible" :close-on-click-modal="false" draggable width="800">
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="排查结果" prop="remarks">
						<el-input
							type="textarea"
							resize="none"
							show-word-limit
							:rows="5"
							v-model="form.remarks"
							maxlength="250"
							placeholder="请输入故障的排查结果"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="文件" prop="annex">
						<div class="w-full">
							<upload-file
								:limit="1"
								:fileSize="3"
								v-model="form.annex"
								ref="upload"
								:fileType="['png', 'jpg', 'jpeg', 'doc', 'pdf', 'docx', 'xlsx', 'xls']"
							/>
						</div>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="FaultHandlingDialog">
import { useMessage } from '/@/hooks/message';
import { addObj } from '/@/api/alarm/alarmFaultHandleLogging';
import type { UploadInstance } from 'element-plus';
const emit = defineEmits(['refresh']);
const upload = ref<UploadInstance>();
// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	alarmFaultLoggingId: '',
	annex: '',
	remarks: '',
});

// 定义校验规则
const dataRules = ref({
	remarks: [{ required: true, message: '请输入排查结果', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.alarmFaultLoggingId = id;

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;
	try {
		loading.value = true;
		await addObj(form);
		useMessage().success('操作成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
